package app.models.system;

import app.Const;
import app.utils.FileUtils;
import com.google.common.base.Strings;
import com.ifonly.activerecord.Model;
import com.ifonly.activerecord.annotation.Table;
import com.ifonly.upload.IFile;
import com.ifonly.utils.DateTimeUtils;

import java.io.File;
import java.util.Date;
import java.util.UUID;

/**
 * @author ifonly
 * @version 1.0 2016-02-19 15:51
 * @since JDK 1.6
 */
@Table(name = "s_attachments")
public class Attachments extends Model {
    private static final long serialVersionUID = 8863820478806831059L;

    private static final int[][] THUMB_SIZES = new int[][] {
            {25, 25},
            {50, 50},
            {100, 100},
            {150, 150},
            {200, 200},
    };

    /**
     * 上传图片保存
     *
     * @param uploadDir 上传文件保存的根路径
     * @param uploadType 上传文件的类型
     * @param attachmentType 附件业务类型
     * @param iFile IFile 对象
     * @return Attachments instance
     */
    public Attachments upload(String uploadDir, String uploadType, String attachmentType, IFile iFile) {
        if (Const.UploadType.IMAGE.name().equalsIgnoreCase(uploadType)) {
            if (Const.AttachmentType.DISH_COVER.name().equalsIgnoreCase(attachmentType)) {
                return saveDishCover(uploadDir, iFile);
            }
        }
        return null;
    }

    /**
     * 菜品信息封面图片
     *  存储方式 : 将文件存储到 {ctx}/uploads/dish_cover/yyyyMMdd/uuid.{suffix}
     *  缩略图存储 :  (25 * 25 | 50 * 50 | 100 * 100 | 200 * 200)
     *      将文件存储到 {ctx}/uploads/dish_cover/yyyy-MM-dd/uuid_xx-xx.{suffix}
     *
     * @param uploadDir 上传文件保存的根路径
     * @param iFile IFile 对象
     */
    private Attachments saveDishCover(String uploadDir, IFile iFile) {

        String fileName = iFile.getOriginalFileName();
        File file = iFile.getFile();
        String suffix = FileUtils.getSuffix(fileName);
        String yyyyMMdd = DateTimeUtils.format(new Date(), "yyyyMMdd");
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");

        String path = "dish_cover" + File.separator + yyyyMMdd + File.separator + uuid + "." + suffix;
        File saveFile = new File(uploadDir + File.separator + path);

        FileUtils.copy(file, saveFile);
        FileUtils.delete(file);

        FileUtils.thumb(saveFile, THUMB_SIZES);

        String md5 = FileUtils.md5(saveFile);

        Attachments attachments = new Attachments();
        attachments.set("name", iFile.getFileName());
        attachments.set("original_name", iFile.getOriginalFileName());
        attachments.set("suffix", suffix);
        attachments.set("md5", md5);
        attachments.set("type", Const.AttachmentType.DISH_COVER.name());
        attachments.set("path", path);
        attachments.set("size", saveFile.length());
        attachments.set("status", Const.Status.NORMAL);
        attachments.saveReturnGeneratedKey();

        return attachments;
    }

    /**
     * 删除附件
     *
     * @param uploadDir 上传文件保存目录
     * @param id 删除的ID
     */
    public void delete(String uploadDir, long id) {
        Attachments attachments = findByPrimaryKey(Attachments.class, id);
        String path = attachments.getString("path");
        File file = new File(uploadDir, path);
        FileUtils.delete(file, THUMB_SIZES);
        attachments.delete();
    }

    public void delete(String uploadDir, String ids) {
        if (!Strings.isNullOrEmpty(ids)) {
            String[] idArr = ids.split(",");
            for (String id: idArr) {
                delete(uploadDir, Long.parseLong(id));
            }
        }
    }
}
